import Header from '../core/header.ts';
import Form from '../core/form.ts';
import Search from '../core/search.ts';
import Button from '../core/Button.ts';
import { SimpleImage } from '../core/upload.ts';

import Trans from '../trans.ts';

import { mappingComponent, mappingField } from './mapping';

class Render {
  private dictionary = {};
  constructor(dictionary) {
    this.dictionary = dictionary;
  }
  search(items: Array = [], values: object = {}) {
    return this.form(items, values, 'search');
  }
  form(items: Array = [], values: object = {}, cla: string = 'form') {
    if (!Array.isArray(items) || items.length === 0) {
      return [];
    }
    let lists = [];
    let words = this.dictionary;
    items.forEach(item => {
      let type = typeof item;
      let form = new Form();
      if (cla === 'search') {
        form = new Search();
      }
      let label = '';
      if (type === 'object' && !Array.isArray(item)) {
        // params 不存在，则外部实例化form后调用了params参数
        if (item.params === undefined) {
          form = item;
        } else {
          form = item.params;
        }
        if (!form.label) {
          form.label = words[form.field] ? words[form.field] : form.field;
        }
        lists.push(form);
      } else {
        if (type === 'string') {
          item = item.split('|');
        }

        let field,
          options,
          component = '';

        let position = item.indexOf('nreq');
        if (position > 0) {
          form.required = false;
          item.splice(position, 1);
        }

        position = item.indexOf('help');
        if (position > 0) {
          form.help = item[position + 1];
          item.splice(position, 2);
        }

        let clear = item.indexOf('clear');
        if (clear > 0) {
          item.splice(clear, 1);
        }

        position = item.indexOf('disabled');
        if (position > 0) {
          item.splice(clear, 1);
          form.disabled = true;
        }

        let len = item.length;

        if (len === 1) {
          field = item[0];
          if (clear < 0) {
            let mapping = mappingField(field);
            component = mapping.type;
            options = mapping.options;
          }
        } else if (len == 2) {
          field = item[0];
          if (typeof item[1] === 'object') {
            component = 'select';
            options = item[1];
          } else {
            component = item[1];
          }
        } else if (len == 3) {
          field = item[0];
          component = item[1];
          options = item[2];
        }

        form.field = field;
        form.label = words[field] ? words[field] : field;
        form = mappingComponent(form, component, options);
        
        if(values[field]!==undefined){
           form.value = values[field];
        }
        
        lists.push(form.params);
      }
    });

    return lists;
  }
  public header(header: Array<any>) {
    let lists = [];
    let words = this.dictionary;
    header.forEach(item => {
      let type = typeof item;
      let header = new Header();
      let label,
        field,
        name,
        solt = '';
      if (type === 'string') {
        // 支持点语法，取对象中的值
        let operator = item.indexOf('.');
        item = item.split('|');
        let len = item.length;
        if (len === 1) {
          field = item[0];
        } else if (len >= 2) {
          field = item[0];
          solt = item[1];
        }
        if (operator >= 0) {
          name = field.replace(/\./g, '_');
        } else {
          name = field;
        }
      } else if (Array.isArray(item) && item.length >= 2) {
        field = item[0];
        solt = item[1];
        name = field;
      }

      label = words[name] ? words[name] : '';
      // 去除_text后缀后匹配label
      if (label.length === 0 && /_text$/.test(name)) {
        name = name.replace(/_text$/, '');
      }
      label = words[name] ? words[name] : '';

      switch (field) {
        case 'avatar':
        case 'head':
          header.setImages();
          break;
        case 'status':
          header.setSwitch();
          break;
      }
      switch (solt) {
        case 'image':
          header.setImages();
          break;
        case 'switch':
          header.setSwitch();
          break;
        case 'edit':
          header.edit = true;
          break;
        case 'action':
          let width = parseInt(item[2]);
          if (isNaN(width) || width <= 80) {
            width = 110;
          }
          header.setAction(width);
          label = '操作';
          break;
        case 'clear':
          header.clear();
          break;
      }
      header.dataIndex = field;
      header.key = field;
      header.title = label;
      lists.push(header.params);
    });
    return lists;
  }

  toolbar(toolbar: any = false): Array {
    let lists = [];
    if (toolbar === 0 || toolbar === false) {
      return [];
    }
    let button = new Button();
    let type = typeof toolbar;
    if (type === 'string') {
      toolbar = toolbar.split('|');
    }
    if (Array.isArray(toolbar)) {
      for (let i in toolbar) {
        let value = toolbar[i];
        if (typeof value === 'object') {
          lists.push(value.params);
        } else if (value === 'plus') {
          lists.push(button.plus());
        } else if (value === 'remove') {
          lists.push(button.remove());
        } else if (value === 'download') {
          lists.push(button.download());
        } else if (value === 'recovery') {
          lists.push(button.recovery());
        } else if (value === 'clean') {
          lists.push(button.clean());
        }
      }
    }
    return lists;
  }

  action(action: any = true) {
    let lists = [];
    let edit = new Button('edit', '编辑', 'icon-park-outline:edit-two').action();
    let remove = new Button('remove', '删除', 'mdi:delete-outline').action();
    let download = new Button('download', '下载', 'mdi:cloud-download-outline').action();
    if (action == true) {
      lists.push(edit);
      lists.push(remove);
    } else {
      let type = typeof action;
      if (type === 'string') {
        action = action.split('|');
      }

      if (Array.isArray(action)) {
        for (let i in action) {
          let value = action[i];
          if (typeof value === 'object') {
            lists.push(value.action());
          } else if (value === 'edit') {
            lists.push(edit);
          } else if (value === 'remove') {
            lists.push(remove);
          } else if (value === 'download') {
            lists.push(download);
          }
        }
      }
    }
    return lists;
  }

  dropActions(action: Array<Button>) {
    let lists = [];
    for (let i in action) {
      lists.push(action[i].action());
    }
    return lists;
  }
}

export default Render;
